<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-17px">
      <el-form-item label="项目编码" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          class="!w-240px"
          placeholder="请输入项目编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="风险类型" prop="type">
        <el-select
          v-model="queryParams.type"
          class="!w-240px"
          placeholder="请选择风险类型"
          @change="handleQuery"
          placement="bottom"
        >
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="风险状态" prop="status">
        <el-select
          v-model="queryParams.status"
          class="!w-240px"
          placeholder="请选择风险状态"
          @change="handleQuery"
          placement="bottom"
        >
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table
      :data="list"
      v-loading="loading"
      empty-text="暂无记录"
      element-loading-text="Loading..."
      stripe
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      show-overflow-tooltip
    >
      <el-table-column align="center" label="项目编号" prop="projectCode" width="180" />
      <el-table-column align="center" label="项目名称" prop="projectName" min-width="200" />
      <el-table-column align="center" label="交付经理" prop="projectManagerName" />
      <!-- <el-table-column align="center" label="项目阶段" prop="projectStage" min-width="120">
        <template #default="scope">
          <div class="flex justify-center">
            <el-tag effect="plain">{{ scope.row.projectStage }} </el-tag>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="风险类型" prop="type" min-width="120" />
      <el-table-column align="center" label="风险状态" prop="status" min-width="120">
        <template #default="scope">
          <div class="flex justify-center">
            <el-tag effect="light" :type="tagTypeObj[scope.row.status]" round
              >{{ stateTypeObj[scope.row.status] }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="120">
        <template #default="scope">
          <el-button link type="primary" @click="handleAudit(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      class="mt-35px text-14px"
      v-model:current-page="queryParams.pageNo"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      background
      :hide-on-single-page="value"
      layout="total,sizes,prev, pager, next,jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as IctApi from '@/api/project/ict'
import { useRouter } from 'vue-router'
import { ref, reactive, onMounted } from 'vue'

const { push } = useRouter() // 路由
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const value = ref(true)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  projectCode: '',
  type: '',
  status: ''
})
const stateTypeObj = {
  Released: '已解除',
  unReleased: '未解除'
}
const tagTypeObj = {
  Released: 'success',
  unReleased: 'danger'
}
const statusList = [
  {
    value: 'Released',
    label: '已解除'
  },
  {
    value: 'unReleased',
    label: '未解除'
  }
]
const typeList = [
  {
    value: 'timely_transfer_risk',
    label: '未及时交底风险'
  },
  {
    value: 'timely_team_risk',
    label: '未及时团队组建风险'
  },
  {
    value: 'timely_delivery_risk',
    label: '未及时内验风险'
  }
]
const queryFormRef = ref()

/** 条数变化 */
const handleSizeChange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
}

/** 页码变化 */
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
}

/** 查询任务列表 */
const getList = async () => {
  IctApi.getRiskPage(queryParams)
    .then((res) => {
      list.value = res.list || []
      total.value = res.total || 0
      if (list.value.length === 0) {
        value.value = true
      } else {
        value.value = false
      }
      loading.value = false
    })
    .catch(() => {
      loading.value = false
    })
}

/** 搜索按钮操作 */
const handleQuery = async () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.pageNo = 1
  getList()
}

/** 处理审批按钮跳转项目移交详情 */
const handleAudit = (row: any) => {
  push({
    name: 'ProjectDetails',
    query: {
      project_code: row.projectCode
    }
  })
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style scoped>
.el-pagination {
  justify-content: flex-end;
}

:deep(.el-tag) {
  display: block;
  line-height: 22px;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>